import 'package:boss/ui/page/pickerAddress/picker_address_page.dart';
import 'package:boss/provider/store_message_provider.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/personalSettings/page/editStoreMessage/modal/storeTypeModal/store_type_modal.dart';
import 'package:boss/ui/page/personalSettings/page/editStoreMessage/provider/edit_store_message_provider.dart';
import 'package:boss/ui/widget/avatar.dart';
import 'package:boss/ui/widget/button/radius_button.dart';
import 'package:boss/ui/widget/custom_list_title.dart';
import 'package:boss/ui/widget/form/base_text_form_field.dart';
import 'package:boss/ui/widget/title_row_button.dart';

import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

class EditStoreMessageBrowser extends StatefulWidget {
  @override
  _EditStoreMessageBrowserState createState() => _EditStoreMessageBrowserState();
}

class _EditStoreMessageBrowserState extends State<EditStoreMessageBrowser> {
  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      padding: EdgeInsets.only(
        left: 30.w,
        right: 30.w,
        top: 10.w,
        bottom: 10.w + MediaQuery.of(context).padding.bottom,
      ),
      child: Consumer<EditStoreMessageProvider>(
        builder: (context, editStoreMessageProvider, _) {
          return Column(
            children: <Widget>[
              Consumer<StoreMessageProvider>(
                builder: (context, storeMessageProvider, _) {
                  return CustomListTitle(
                    title: '门店头像',
                    trailing: Avatar(
                      image: storeMessageProvider.storeMessage?.storeHeadImage,
                      size: 80.w,
                    ),
                  );
                },
              ),
              CustomListTitle(
                title: '商户编号',
                trailing: Text(
                  editStoreMessageProvider.storeMessage.storeNo,
                  style: TextStyleConstant().normal_4_28,
                ),
              ),
              BaseTextFormField(
                prefixText: '门店名称',
                initialValue: editStoreMessageProvider.storeMessage.storeName,
                onChanged: (value) {
                  editStoreMessageProvider.storeMessage.storeName = value;
                },
              ),
              TitleRowButton(
                title: '门店种类',
                value: editStoreMessageProvider.storeMessage.storeTypeName,
                onTap: () async {
                  showStoreTypeModal(
                    context,
                    defaultValue: editStoreMessageProvider.storeMessage.storeTypeId,
                    onChanged: (value) {
                      editStoreMessageProvider.setStoreType(value);
                    },
                  );
                },
              ),
              TitleRowButton(
                title: '所在区域',
                hintText: '请选择区域',
                value: editStoreMessageProvider.address,
                onTap: () async {
                  double latitude;
                  double longitude;
                  try {
                    latitude = double.parse(editStoreMessageProvider.storeMessage.latitude);
                    longitude = double.parse(editStoreMessageProvider.storeMessage.longitude);
                  } catch (e) {}

                  final res = await showPickerAddressPage(
                    context,
                    data: PCDHData(
                      provinces: editStoreMessageProvider.storeMessage.province,
                      provincesCode: editStoreMessageProvider.storeMessage.provinceCode,
                      city: editStoreMessageProvider.storeMessage.city,
                      cityCode: editStoreMessageProvider.storeMessage.cityCode,
                      district: editStoreMessageProvider.storeMessage.district,
                      districtCode: editStoreMessageProvider.storeMessage.districtCode,
                      address: editStoreMessageProvider.storeMessage.address,
                      houseNumber: editStoreMessageProvider.storeMessage.houseNumber,
                      latitude: latitude,
                      longitude: longitude,
                    ),
                  );

                  if (res != null) {
                    editStoreMessageProvider.setStoreAddress(res);
                  }
                },
              ),
              BaseTextFormField(
                prefixText: '联系手机',
                initialValue: editStoreMessageProvider.storeMessage.phone,
                keyboardType: TextInputType.number,
                inputFormatters: [FilteringTextInputFormatter.allow(RegUtil.integerReg)],
                onChanged: (value) {
                  editStoreMessageProvider.storeMessage.phone = value;
                },
              ),
              BaseTextFormField(
                prefixText: '邮箱',
                initialValue: editStoreMessageProvider.storeMessage.email,
                onChanged: (value) {
                  editStoreMessageProvider.storeMessage.email = value;
                },
              ),
              RadiusButton(
                title: '保 存',
                margin: EdgeInsets.only(top: 40.w),
                width: 590.w,
                onTap: () async {
                  final storeMessageProvider = Provider.of<StoreMessageProvider>(context, listen: false);

                  storeMessageProvider.updateStoreMessage(editStoreMessageProvider.storeMessage);
                },
              ),
            ],
          );
        },
      ),
    );
  }
}
